package com.kyc.kycxkplaygame.activity;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.kyc.kycxkplaygame.R;
import com.kyc.kycxkplaygame.dbutils.GetPropLevelUtils;
import com.kyc.kycxkplaygame.dbutils.IntImp.AlasbusyIApi;
import com.kyc.kycxkplaygame.dbutils.inter.HttpQuestResultListener;
import com.kyc.kycxkplaygame.entity.PropsCode;
import com.kyc.kycxkplaygame.util.LevelData;
import com.kyc.kycxkplaygame.util.SharedPreferencesUtils;
import com.kyc.kycxkplaygame.util.TimeUtil;
import com.zhy.android.percent.support.PercentLinearLayout;
import com.zhy.android.percent.support.PercentRelativeLayout;
import es.dmoral.toasty.Toasty;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class GameActivity extends AppCompatActivity implements View.OnClickListener, HttpQuestResultListener {
    private LinearLayout lin_layout;
    //private PercentFrameLayout lin_layout;

    public static GameActivity instanse;

    //public int[] Image_level_01 = {R.drawable.icecream_01,R.drawable.icecream_02,R.drawable.icecream_03};
    private int isID = -1;

    //public int isVisiable = 0;
    private ProgressBar progressBar;

    public int progress = 0;

    private int[] intsLevel;

    private int fraction = 0;  //分数

    private int batter = 0;  //连击

    private int grape_currency = 0; //葡萄币

    private Date startTime = null;
    private Date endTime = null;

    //private List<Integer> intsLevel = new ArrayList<>();   //图片数据
    private List<ImageView> imageList = new ArrayList<>();  //按钮数据
    private List<ImageView> inVisiableImage = new ArrayList<>();
    private String level_num;

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    private ImageView button;
    private ImageView newbutton;
    private TextView level_name;
    private int num;
    private PercentRelativeLayout zhadan;
    private TextView zhadan_num;
    private PercentRelativeLayout tishi;
    private TextView tishi_num;
    private ProgressBar top_progressBar;
    /* private ImageView three;
     private ImageView one;
     private ImageView two;*/
    private TextView time_num;
    private PercentRelativeLayout time;
    private ImageView time_daoji;
    private FrameLayout fragment;

    int daojiTime = 0;

    int xinCount = 0;


    private double CountFraction = 0;  //总分数
    private TextView fraction_666;
    //private String tili_num;
    //private int newtili;
    private Animation animation_bg;
    private ImageView daoju_anim;
    private FrameLayout fragment_anim;
    private long second = 0;   //秒
    private boolean isEable = true;
    private boolean isBatterFrist = true;
    private FrameLayout fragment_lianji;
    private TextView lianji_image_txt;
    private TextView game_batter_num;
    private String status;
    private MediaPlayer mPlayer;
    private boolean yx_open;
    private AlasbusyIApi alasbusyIApi;
    // private int xiyou;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        instanse = this;
        level_num = getIntent().getStringExtra("level_num");
        status = getIntent().getStringExtra("status");

        initView();

        level_name.setText("第" + level_num + "关");

        fragment.setVisibility(View.VISIBLE);
        handler.postDelayed(DaojiRunnable, 500);
        yx_open = (boolean) SharedPreferencesUtils.getParam(this, "yx_open", true);
        if(yx_open) {
           play(R.raw.daojishi);
        }
        initListener();
    }

    private boolean isDaojiTime = false;

    Runnable DaojiRunnable = new Runnable() {
        @Override
        public void run() {
            if (daojiTime >= 3) {
                if(yx_open) {
                    stop();
                }
                fragment.setVisibility(View.GONE);
                handler.removeCallbacks(DaojiRunnable);
                getLevelImage();
                isDaojiTime = true;
                if(yx_open) {
                    play( R.raw.m_bg2);
                }
            } else {
                time_daoji.setImageResource(LevelData.daojishi[daojiTime]);
                handler.postDelayed(DaojiRunnable, 1000);
                isDaojiTime = false;
            }
            daojiTime++;
        }
    };

    private void initView() {
        lin_layout = (PercentLinearLayout) findViewById(R.id.lin_layout);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        level_name = (TextView) findViewById(R.id.level_name);
        progressBar.setProgress(0);

        top_progressBar = (ProgressBar) findViewById(R.id.top_progressBar);
        top_progressBar.setProgress(0);
       /* three = (ImageView) findViewById(R.id.three);
        one = (ImageView) findViewById(R.id.one);
        two = (ImageView) findViewById(R.id.two);*/

        fraction_666 = (TextView) findViewById(R.id.fraction_666);
        fraction_666.setText("0");
        //top_progressBar.setProgress(0);

        daoju_anim = findViewById(R.id.daoju_anim);
        fragment_anim = findViewById(R.id.fragment_anim);

        time_daoji = (ImageView) findViewById(R.id.time_daoji);
        fragment = (FrameLayout) findViewById(R.id.fragment_Center);

        fragment_lianji = findViewById(R.id.fragment_lianji);
        lianji_image_txt = findViewById(R.id.lianji_image_txt);

        game_batter_num = findViewById(R.id.game_batter_num);
        game_batter_num.setText("0");
        //道具
        zhadan = (PercentRelativeLayout) findViewById(R.id.zhadan);
        zhadan_num = (TextView) findViewById(R.id.zhadan_num);

        tishi = (PercentRelativeLayout) findViewById(R.id.tishi);
        tishi_num = (TextView) findViewById(R.id.tishi_num);

        time = (PercentRelativeLayout) findViewById(R.id.times);
        time_num = (TextView) findViewById(R.id.time_num);

        //先获得体力的数量,再减一
        setTiliNum();

        int zhandan = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("炸弹")); //(int) SharedPreferencesUtils.getParam(GameActivity.this, "zhandan", 0);//.setParam(GameActivity.this, "level", Integer.parseInt(level_num));
        if (zhandan > 0) {
            zhadan_num.setVisibility(View.VISIBLE);
            zhadan_num.setText(String.valueOf(zhandan));
        } else {
            zhadan_num.setVisibility(View.INVISIBLE);
            zhadan_num.setText(String.valueOf(0));
        }

        int tishi = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("提示"));//(int) SharedPreferencesUtils.getParam(GameActivity.this, "tishi", 0);
        if (tishi > 0) {
            tishi_num.setVisibility(View.VISIBLE);
            tishi_num.setText(String.valueOf(tishi));
        } else {
            tishi_num.setVisibility(View.INVISIBLE);
            tishi_num.setText(String.valueOf(0));
        }

        int time = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("时间"));//(int) SharedPreferencesUtils.getParam(GameActivity.this, "time", 0);
        if (time > 0) {
            time_num.setVisibility(View.VISIBLE);
            time_num.setText(String.valueOf(time));
        } else {
            time_num.setVisibility(View.INVISIBLE);
            time_num.setText(String.valueOf(0));
        }

    }

    private void getLevelImage() {
        if (!TextUtils.isEmpty(level_num)) {
            if (level_num.equals("01")) {
                progressBar.setMax(10);
                //top_progressBar.setMax(10000);
                intsLevel = new int[6];
                CountFraction = ((3 + 10) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_01, R.drawable.icecream_03, 3);
                    setListData(ints, j);
                }
            } else if (level_num.equals("02")) {
                progressBar.setMax(10);
                //top_progressBar.setMax(10000);
                intsLevel = new int[8];
                CountFraction = ((4 + 10) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_01, R.drawable.icecream_04, 4);
                    setListData(ints, j);
                }
            } else if (level_num.equals("03")) {
                progressBar.setMax(30);
                //top_progressBar.setMax(12000);
                intsLevel = new int[12];
                CountFraction = ((6 + 30) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_01, R.drawable.icecream_06, 6);
                    setListData(ints, j);
                }
            } else if (level_num.equals("04")) {
                progressBar.setMax(35);
                //top_progressBar.setMax(15000);
                intsLevel = new int[16];
                CountFraction = ((8 + 35) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_01, R.drawable.icecream_08, 8);
                    setListData(ints, j);
                }
            } else if (level_num.equals("05")) {
                progressBar.setMax(35);
                //top_progressBar.setMax(18000);
                intsLevel = new int[20];
                CountFraction = ((10 + 35) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_01, R.drawable.icecream_10, 10);
                    setListData(ints, j);
                }
            } else if (level_num.equals("06")) {
                progressBar.setMax(40);
                //top_progressBar.setMax(20000);
                intsLevel = new int[24];
                CountFraction = ((12 + 40) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_01, R.drawable.icecream_12, 12);
                    setListData(ints, j);
                }
            } else if (level_num.equals("07")) {
                progressBar.setMax(50);
                //top_progressBar.setMax(26000);
                intsLevel = new int[30];
                CountFraction = ((15 + 50) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_01, R.drawable.icecream_15, 15);
                    for (int i = 0; i < ints.length; i++) {
                        if (j == 1) {
                            int a = ints.length + i;
                            intsLevel[a] = ints[i];
                        } else {
                            intsLevel[i] = ints[i];
                        }
                    }
                }
            } else if (level_num.equals("08")) {
                progressBar.setMax(60);
                // top_progressBar.setMax(36000);
                intsLevel = new int[36];
                CountFraction = ((18 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_01, R.drawable.icecream_18, 18);
                    setListData(ints, j);
                }
            } else if (level_num.equals("09")) {
                progressBar.setMax(60);
                //top_progressBar.setMax(36000);
                intsLevel = new int[36];
                CountFraction = ((18 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_03, R.drawable.icecream_20, 18);
                    setListData(ints, j);
                }
            } else if (level_num.equals("10")) {
                progressBar.setMax(60);
                //top_progressBar.setMax(36000);
                intsLevel = new int[36];
                CountFraction = ((18 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_06, R.drawable.icecream_23, 18);
                    setListData(ints, j);
                }
            } else if (level_num.equals("11")) {
                progressBar.setMax(60);
                // top_progressBar.setMax(42000);
                intsLevel = new int[42];
                CountFraction = ((21 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_06, R.drawable.icecream_26, 21);
                    setListData(ints, j);
                }
            } else if (level_num.equals("12")) {
                progressBar.setMax(60);
                //top_progressBar.setMax(42000);
                intsLevel = new int[42];
                CountFraction = ((21 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_07, R.drawable.icecream_27, 21);
                    setListData(ints, j);
                }
            } else if (level_num.equals("13")) {
                progressBar.setMax(60);
                //top_progressBar.setMax(42000);
                intsLevel = new int[42];
                CountFraction = ((21 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.xiari_01, R.drawable.xiari_21, 21);
                    setListData(ints, j);
                }
            } else if (level_num.equals("14")) {
                progressBar.setMax(60);
                //top_progressBar.setMax(42000);
                intsLevel = new int[36];
                CountFraction = ((18 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.food_icons_01, R.drawable.food_icons_18, 18);
                    setListData(ints, j);
                }
            } else if (level_num.equals("15")) {
                progressBar.setMax(60);
                //top_progressBar.setMax(42000);
                intsLevel = new int[42];
                CountFraction = ((21 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.xiari_01, R.drawable.xiari_21, 21);
                    setListData(ints, j);
                }
            } else if (level_num.equals("16")) {
                progressBar.setMax(60);
                //top_progressBar.setMax(42000);
                intsLevel = new int[48];
                CountFraction = ((24 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.xiari_01, R.drawable.xiari_21, 21);
                    //setListData(ints, j);

                    int[] foodints = LevelData.randomArray(R.drawable.food_icons_01, R.drawable.food_icons_03, 3);
                    /*int[] result = new int[ints.length + foodints.length];*/
                    /*System.arraycopy(ints, 0, result, 0, ints.length);
                    System.arraycopy(foodints, 0, result, ints.length, foodints.length);*/
                    int[] concat = getConcat(ints, foodints);
                    setListData(concat, j);
                }
            } else if (level_num.equals("17")) {
                progressBar.setMax(60);
                intsLevel = new int[48];
                CountFraction = ((24 + 60) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.xiari_11, R.drawable.xiari_21, 11);
                    int[] foodints = LevelData.randomArray(R.drawable.food_icons_01, R.drawable.food_icons_13, 13);
                    int[] concat = getConcat(ints, foodints);
                    setListData(concat, j);
                }
            } else if (level_num.equals("18")) {
                progressBar.setMax(65);
                intsLevel = new int[50];
                CountFraction = ((25 + 65) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_04, R.drawable.icecream_10, 7);
                    int[] foodints = LevelData.randomArray(R.drawable.food_icons_01, R.drawable.food_icons_18, 18);
                    int[] concat = getConcat(ints, foodints);
                    setListData(concat, j);
                }
            } else if (level_num.equals("19")) {
                progressBar.setMax(65);
                intsLevel = new int[50];
                CountFraction = ((25 + 65) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_11, R.drawable.icecream_17, 7);
                    int[] foodints = LevelData.randomArray(R.drawable.food_icons_01, R.drawable.food_icons_18, 18);
                    int[] concat = getConcat(ints, foodints);
                    setListData(concat, j);
                }
            } else if (level_num.equals("20")) {
                progressBar.setMax(65);
                intsLevel = new int[50];
                CountFraction = ((25 + 65) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_05, R.drawable.icecream_25, 21);
                    int[] foodints = LevelData.randomArray(R.drawable.food_icons_03, R.drawable.food_icons_06, 4);
                    int[] concat = getConcat(ints, foodints);
                    setListData(concat, j);
                }
            } else if (level_num.equals("21")) {
                progressBar.setMax(65);
                intsLevel = new int[52];
                CountFraction = ((26 + 65) * 100) / 1.5;
                top_progressBar.setMax((int) CountFraction);
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(R.drawable.icecream_10, R.drawable.icecream_30, 21);
                    int[] foodints = LevelData.randomArray(R.drawable.food_icons_03, R.drawable.food_icons_07, 5);
                    int[] concat = getConcat(ints, foodints);
                    setListData(concat, j);
                }
            } else {
                startActivity(new Intent(this, LevelActivity.class));
                GameActivity.this.finish();
                return;
            }
            progressBar.setProgress(progress);
            top_progressBar.setProgress(fraction);
            fraction_666.setText(fraction + "");
            Log.i("TAG", "lenght === " + intsLevel.length);
            createLayout();
            handler.postDelayed(progressRunnable, 1000);
        } else {
            startActivity(new Intent(this, LevelActivity.class));
            GameActivity.this.finish();
        }
    }

    private void initListener() {
        zhadan.setOnClickListener(this);

        tishi.setOnClickListener(this);

        time.setOnClickListener(this);

        findViewById(R.id.play_game_pause).setOnClickListener(this);

        findViewById(R.id.back_game).setOnClickListener(this);

        alasbusyIApi = AlasbusyIApi.getInstance(this);
        alasbusyIApi.setHttpQuestResultListener(this);
    }


    //合并数组
    public int[] getConcat(int[] ints, int[] twos) {
        int[] result = new int[ints.length + twos.length];
        System.arraycopy(ints, 0, result, 0, ints.length);
        System.arraycopy(twos, 0, result, ints.length, twos.length);
        return result;
    }

    public void setListData(int[] ints, int j) {
        for (int i = 0; i < ints.length; i++) {
            if (j == 1) {
                int a = ints.length + i;
                intsLevel[a] = ints[i];
            } else {
                intsLevel[i] = ints[i];
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        yx_open = (boolean) SharedPreferencesUtils.getParam(this, "yx_open", true);
    }

    @Override
    protected void onPause() {
        handler.removeCallbacks(progressRunnable);
        super.onPause();
       // pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        yx_open = (boolean) SharedPreferencesUtils.getParam(this, "yx_open", true);
        if (yx_open) {
            pause();
        }else {
            stop();
        }
    }

    private void createLayout() {
        LinearLayout[] layouts;
        if (level_num.equals("01")) {
            layouts = new LinearLayout[intsLevel.length / 2];
            for (int j = 0; j < intsLevel.length / 2; j++) {
                LinearLayout layout_01 = new LinearLayout(this);
                layout_01.setOrientation(LinearLayout.HORIZONTAL);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.weight = 1;
                params.gravity = Gravity.CENTER;
                layout_01.setLayoutParams(params);
                layouts[j] = layout_01;
            }
        } else if (Integer.parseInt(level_num) >= 7 && Integer.parseInt(level_num) <= 15) {
            layouts = new LinearLayout[6];
            for (int j = 0; j < 6; j++) {
                LinearLayout layout_01 = new LinearLayout(this);
                layout_01.setOrientation(LinearLayout.HORIZONTAL);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.weight = 1;
                if (j == 0) {
                    params.topMargin = 50;
                }
                if (j == 5) {
                    params.bottomMargin = 50;
                }
                params.leftMargin = 10;
                params.rightMargin = 10;
                params.gravity = Gravity.CENTER;
                layout_01.setLayoutParams(params);
                layouts[j] = layout_01;
            }
        } else if (Integer.parseInt(level_num) > 15 && Integer.parseInt(level_num) < 25) {
            layouts = new LinearLayout[8];
            for (int j = 0; j < 8; j++) {
                LinearLayout layout_01 = new LinearLayout(this);
                layout_01.setOrientation(LinearLayout.HORIZONTAL);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.weight = 1;
                if (j == 0) {
                    params.topMargin = 50;

                } else if (j == 7) {
                    params.bottomMargin = 50;
                }
                params.leftMargin = 10;
                params.rightMargin = 10;
                params.gravity = Gravity.CENTER;
                layout_01.setLayoutParams(params);
                layouts[j] = layout_01;
            }
        } else {
            layouts = new LinearLayout[Integer.parseInt(level_num)];
            for (int j = 0; j < Integer.parseInt(level_num); j++) {
                LinearLayout layout_01 = new LinearLayout(this);
                layout_01.setOrientation(LinearLayout.HORIZONTAL);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.weight = 1;
                params.gravity = Gravity.CENTER;
                layout_01.setLayoutParams(params);
                layouts[j] = layout_01;
            }
        }
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        params.gravity = Gravity.CENTER;
        if (Integer.parseInt(level_num) >= 7 && Integer.parseInt(level_num) < 11) {
            num = 6;
        } else if (Integer.parseInt(level_num) >= 11 && (Integer.parseInt(level_num) <= 15)) {
            num = 7;
        } else if (Integer.parseInt(level_num) > 15 && Integer.parseInt(level_num) < 25) {
            num = 8;
        } else {
            num = intsLevel.length / Integer.parseInt(level_num);
        }
        imageList.clear();
        inVisiableImage.clear();
        for (int i = 0; i < intsLevel.length; i++) {
            final ImageView image = new ImageView(this);
            if (Integer.parseInt(level_num) >= 14 && Integer.parseInt(level_num) < 25) {
                image.setImageResource(intsLevel[i]);
            } else {
                image.setImageResource(R.drawable.image);
            }
            image.setLayoutParams(params);
            image.setPadding(10, 10, 10, 10);
            image.setId(i);
            imageList.add(image);
            if (level_num.equals("01")) {
                if (i < 3) {
                    layouts[0].addView(image);
                } else {
                    layouts[1].addView(image);
                }
            } else {
                if (i < num) {
                    layouts[0].addView(image);
                } else if (i >= num && i < num * 2) {
                    layouts[1].addView(image);
                } else if (i >= num * 2 && i < num * 3) {
                    layouts[2].addView(image);
                } else if (i >= num * 3 && i < num * 4) {
                    layouts[3].addView(image);
                } else if (i >= num * 4 && i < num * 5) {
                    layouts[4].addView(image);
                } else if (i >= num * 5 && i < num * 6) {
                    layouts[5].addView(image);
                } else if (i >= num * 6 && i < num * 7) {
                    layouts[6].addView(image);
                } else if (i >= num * 7 && i < num * 8) {
                    layouts[7].addView(image);
                } else if (i >= num * 8 && i < num * 9) {
                    layouts[8].addView(image);
                } else {
                    layouts[layouts.length - 1].addView(image);
                }
            }
            final int finalI = i;

            image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isEable) {
                        int id = view.getId();
                        if (isID == id) {

                        } else {
                            if (isID != -1) {
                                button = (ImageView) findViewById(isID);
                                newbutton = (ImageView) findViewById(id);
                                if (finalI >= intsLevel.length) {
                                    if (Integer.parseInt(level_num) < 14) {
                                        button.setImageResource(intsLevel[finalI - intsLevel.length]);
                                        newbutton.setImageResource(intsLevel[finalI - intsLevel.length]);
                                    }
                                    if (intsLevel[finalI - intsLevel.length] == intsLevel[isID]) {
                                        handler.postDelayed(visiableRunnable, 100);
                                    } else {
                                        if (Integer.parseInt(level_num) < 14) {
                                            button.setImageResource(R.drawable.image);
                                            newbutton.setImageResource(R.drawable.image);
                                        }
                                        button.setBackgroundResource(R.drawable.img_normal_border);
                                        newbutton.setBackgroundResource(R.drawable.img_press_border);
                                    }
                                } else {
                                    if (Integer.parseInt(level_num) < 14) {
                                        button.setImageResource(intsLevel[finalI]);
                                        newbutton.setImageResource(intsLevel[finalI]);
                                    }
                                    if (intsLevel[finalI] == intsLevel[isID]) {
                                        handler.postDelayed(visiableRunnable, 100);
                                    } else {
                                        if (Integer.parseInt(level_num) < 14) {
                                            button.setImageResource(R.drawable.image);
                                            newbutton.setImageResource(R.drawable.image);
                                        }
                                        button.setBackgroundResource(R.drawable.img_normal_border);
                                        newbutton.setBackgroundResource(R.drawable.img_press_border);
                                    }
                                }
                                newbutton.setBackgroundResource(R.drawable.img_press_border);
                            }
                            ImageView button = (ImageView) findViewById(id);
                            if (finalI >= intsLevel.length) {
                                button.setImageResource(intsLevel[finalI - intsLevel.length]);
                            } else {
                                if (Integer.parseInt(level_num) < 14) {
                                    button.setImageResource(intsLevel[finalI]);
                                }
                            }
                            button.setBackgroundResource(R.drawable.img_press_border);
                        }
                        isID = id;
                    }
                }
            });
        }

        for (int i = 0; i < layouts.length; i++) {
            lin_layout.addView(layouts[i]);
        }
    }

    Runnable progressRunnable = new Runnable() {
        @Override
        public void run() {
            progress = progress + 1;
            if (progress >= progressBar.getMax()) {
                if (imageList.size() > 0) {
                    progressBar.setProgress(progress);
                    top_progressBar.setProgress(fraction);
                    handler.removeCallbacks(progressRunnable);
                    Intent intent = new Intent(GameActivity.this, ClearanceActivity.class);
                    intent.putExtra("clearance", false);
                    intent.putExtra("levle", level_num);
                    intent.putExtra("fraction", String.valueOf(xinCount));
                    intent.putExtra("fraction666", String.valueOf(fraction));
                    intent.putExtra("batter", String.valueOf(batter));
                    intent.putExtra("grape", String.valueOf(grape_currency));
                    startActivityForResult(intent, 300);
                }
            } else {
                progressBar.setProgress(progress);
                top_progressBar.setProgress(fraction);
                handler.postDelayed(progressRunnable, 1000);
            }
        }
    };

    Runnable visiableRunnable = new Runnable() {
        @Override
        public void run() {
            button.setBackgroundResource(R.drawable.img_normal_border);
            newbutton.setBackgroundResource(R.drawable.img_normal_border);
            button.setVisibility(View.INVISIBLE);
            newbutton.setVisibility(View.INVISIBLE);

            if (isBatterFrist) {
                String nowEndTime = TimeUtil.getNowYMDHMSTime();
                SimpleDateFormat CurrentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    startTime = CurrentTime.parse(nowEndTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                SharedPreferencesUtils.setParam(GameActivity.this, "lianji_time", startTime.getTime());
                isBatterFrist = false;
                batter = 0;
                fraction = fraction + 100;
                grape_currency = grape_currency + 10;
            } else {
                String nowEndTime = TimeUtil.getNowYMDHMSTime();
                SimpleDateFormat CurrentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    endTime = CurrentTime.parse(nowEndTime);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                long lianji_time = (long) SharedPreferencesUtils.getParam(GameActivity.this, "lianji_time", 0L);
                if (lianji_time < endTime.getTime()) {
                    second = ((endTime.getTime() - lianji_time) / 1000);
                } else {
                    second = ((lianji_time - endTime.getTime()) / 1000);
                }
                if (second <= 1) {
                    batter += 1;
                    fraction = fraction + (100 * batter);
                    grape_currency = grape_currency + (10 * batter);
                    lianjiAnim();
                    lianji_image_txt.setText("+" + batter);
                    game_batter_num.setText("" + batter);
                } else {
                    batter = 0;
                    fraction = fraction + 100;
                    grape_currency = grape_currency + 10;
                }
                SharedPreferencesUtils.setParam(GameActivity.this, "lianji_time", endTime.getTime());
            }
            //top_progressBar.setProgress(fraction);
            getFraction(fraction);
            imageList.remove(button);
            imageList.remove(newbutton);
            inVisiableImage.add(button);
            inVisiableImage.add(newbutton);
            if (Integer.parseInt(level_num) >= 14 && Integer.parseInt(level_num) < 17) {
                addImage(level_num, R.drawable.icecream_28, R.drawable.icecream_31, 4);
            } else if (Integer.parseInt(level_num) >= 17 && Integer.parseInt(level_num) < 22) {
                addImage(level_num, R.drawable.xiari_01, R.drawable.xiari_08, 8);
            } else if (Integer.parseInt(level_num) >= 22 && progress < progressBar.getMax() / 2) {
                addImage(level_num, R.drawable.xiari_01, R.drawable.xiari_06, 6);
            }

            Clearance();
        }
    };

    //连击效果
    public void lianjiAnim() {
        Animation loadAnimation = AnimationUtils.loadAnimation(GameActivity.this, R.anim.lianji_anim);
        fragment_lianji.setVisibility(View.VISIBLE);
        fragment_lianji.startAnimation(loadAnimation);
        loadAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                fragment_lianji.clearAnimation();
                fragment_lianji.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    //获取分数显示红心
    private void getFraction(int fa) {
        if (fa >= (intsLevel.length / 2) * 100 && fa < CountFraction / 1.5 /*(intsLevel.length / 2 + 1) * 100 && fa < (intsLevel.length / 2 + 2) * 100*/) {
            xinCount = 1;
        } else if (fa >= CountFraction / 1.5 && fa < CountFraction / 1.2/*(intsLevel.length / 2 + 2) * 100 && fa < (intsLevel.length + 2) * 100*/) {
            xinCount = 2;
        } else if (fa >= CountFraction / 1.2 /*(intsLevel.length + 2) * 100*/) {
            xinCount = 3;
            Log.i("TAG", "xinCount == " + xinCount);
        }
        top_progressBar.setProgress(fa);
        fraction_666.setText(fa + "");
    }

    @Override
    public void onBackPressed() {
        handler.removeCallbacks(progressRunnable);
        handler.removeCallbacks(DaojiRunnable);
        LevelActivity.instanse.finish();
        startActivity(new Intent(this, LevelActivity.class));
        GameActivity.this.finish();
    }

    public void Clearance() {
        if (imageList.size() == 0 && progress < progressBar.getMax()) {
            int level = GetPropLevelUtils.getInstance().getLevel();//(int) SharedPreferencesUtils.getParam(GameActivity.this, "level", 0);
            if (level < Integer.parseInt(level_num)) {
                //setParam(GameActivity.this, "level", Integer.parseInt(level_num));
                //String phone = (String) SharedPreferencesUtils.getParam(GameActivity.this, "phone", "");
                GetPropLevelUtils.getInstance().updateLevel(level_num);
            }
            int time = progressBar.getMax() - progress;

            fraction = fraction + time * 100;
            getFraction(fraction);
            handler.removeCallbacks(progressRunnable);

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(GameActivity.this, ClearanceActivity.class);
                    intent.putExtra("clearance", true);
                    intent.putExtra("levle", level_num);
                    intent.putExtra("fraction", String.valueOf(xinCount));
                    intent.putExtra("fraction666", String.valueOf(fraction));
                    intent.putExtra("batter", String.valueOf(batter));
                    intent.putExtra("grape", String.valueOf(grape_currency));
                    startActivityForResult(intent, 300);
                }
            },1000);
        }
    }

    public boolean isFirst = true;

    public void addImage(String level_num, int min, int max, int len) {
        // if (Integer.parseInt(level_num) >= 9) {
        if (isFirst) {
            if ((imageList.size() == 18 && (Integer.parseInt(level_num) >= 14 && Integer.parseInt(level_num) < 17) ||
                    (imageList.size() == 16 && (Integer.parseInt(level_num) >= 17 && Integer.parseInt(level_num) < 22))
                    || (imageList.size() == 12 && Integer.parseInt(level_num) >= 22 && progress < progressBar.getMax() / 2))) {
                for (int j = 0; j < 2; j++) {
                    int[] ints = LevelData.randomArray(min, max, len);
                    //setListData(ints,j);
                    addNanDu(ints, j);
                    CountFraction += len * 100;
                }
            }
        }
        // }
    }

    public void addNanDu(int[] ints, int j) {
        int id;
        for (int i = 0; i < ints.length; i++) {
            if (j == 1) {
                int a = ints.length + i;
                //intsLevel[a] = ints[i];
                id = inVisiableImage.get(a).getId();
                intsLevel[id] = ints[i];
            } else {
                id = inVisiableImage.get(i).getId();
                intsLevel[id] = ints[i];
            }
            //Log.i("TAG"," addnandu  id ==== " + id);
            ImageView imageView = (ImageView) findViewById(id);
            if (Integer.parseInt(level_num) >= 14 && Integer.parseInt(level_num) < 25) {
                imageView.setImageResource(intsLevel[id]);
            } else {
                imageView.setImageResource(R.drawable.image);
            }
            imageView.setBackgroundResource(R.drawable.img_normal_border);
            imageView.setVisibility(View.VISIBLE);
            imageList.add(imageView);
        }
        isFirst = false;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 200) {
            if (data == null) {
                return;
            }
            String num = data.getStringExtra("num");
            zhadan.setEnabled(true);
            if (num.equals("0")) {
                zhadan_num.setVisibility(View.INVISIBLE);
            } else {
                zhadan_num.setVisibility(View.VISIBLE);
            }
            //setParam(GameActivity.this, "zhandan", Integer.parseInt(num));
            GetPropLevelUtils.getInstance().updateNoFriutPropNum("炸弹",Integer.parseInt(num));
            alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"炸弹",Integer.parseInt(num));
            zhadan_num.setText(num);
            handler.postDelayed(progressRunnable, 1000);
        } else if (requestCode == 201) {
            if (data == null) {
                return;
            }
            String num = data.getStringExtra("num");
            if (num.equals("0")) {
                tishi_num.setVisibility(View.INVISIBLE);
            } else {
                tishi_num.setVisibility(View.VISIBLE);
            }
            //setParam(GameActivity.this, "tishi", Integer.parseInt(num));
            GetPropLevelUtils.getInstance().updateNoFriutPropNum("提示",Integer.parseInt(num));
            alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"提示",Integer.parseInt(num));
            tishi_num.setText(num);
            handler.postDelayed(progressRunnable, 1000);
        } else if (requestCode == 300) {
            if (data == null) {
                return;
            }
            lin_layout.removeAllViews();
            lin_layout.addView(fragment);
            level_num = data.getStringExtra("level_num");
            fraction = 0;
            progress = 0;
            daojiTime = 0;
            xinCount = 0;
            CountFraction = 0;
            batter = 0;
            grape_currency = 0;
            isFirst = true;
            setTiliNum();
            initView();
            level_name.setText("第" + level_num + "关");
            fragment.setVisibility(View.VISIBLE);
            time_daoji.setImageResource(LevelData.daojishi[0]);
            handler.postDelayed(DaojiRunnable, 500);
            initListener();
        } else if (requestCode == 202) {
            String num = data.getStringExtra("num");
            if (num.equals("0")) {
                time_num.setVisibility(View.INVISIBLE);
            } else {
                time_num.setVisibility(View.VISIBLE);
            }
            //setParam(GameActivity.this, "time", Integer.parseInt(num));
            GetPropLevelUtils.getInstance().updateNoFriutPropNum("时间",Integer.parseInt(num));
            alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"时间",Integer.parseInt(num));
            time_num.setText(num);
            handler.postDelayed(progressRunnable, 1000);
        } else if (requestCode == 500) {
            String pause = data.getStringExtra("pause");
            if (pause.equals("tili")) {
               status = pause;
                setTiliNum();
                progress = 0;
                progressBar.setProgress(progress);
            }else if(pause.equals("xiyou")) {
                status = pause;
                setTiliNum();
                progress = 0;
                progressBar.setProgress(progress);
            }
            handler.postDelayed(progressRunnable, 1000);
            yx_open = (boolean) SharedPreferencesUtils.getParam(this, "yx_open", true);
            if(yx_open) {
                play( R.raw.m_bg2);
            }
        } else if (requestCode == 700) {
            //setTiliNum();
            yx_open = (boolean) SharedPreferencesUtils.getParam(this, "yx_open", true);
            if(yx_open) {
                play( R.raw.m_bg2);
            }
            String pause = data.getStringExtra("pause");
            if(pause.equals("close")) {
                onBackPressed();
            }else {
                handler.postDelayed(progressRunnable, 1000);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.times:
                if (isDaojiTime) {
                    daojuTimes();
                }
                break;
            case R.id.zhadan:
                if (isDaojiTime) {
                    daojuZhadan();
                }
                break;
            case R.id.tishi:
                if (isDaojiTime) {
                    daojuTishi();
                }
                break;
            case R.id.play_game_pause:
                if (isDaojiTime) {
                    yx_open = (boolean) SharedPreferencesUtils.getParam(this, "yx_open", true);
                    if (yx_open) {
                        pause();
                    }
                    Intent intent = new Intent(GameActivity.this, PauseGameActivity.class);
                    intent.putExtra("close", "pause");
                    startActivityForResult(intent, 500);
                }
                break;
            case R.id.back_game:
                //退出当前游戏
                if (isDaojiTime) {
                    yx_open = (boolean) SharedPreferencesUtils.getParam(this, "yx_open", true);
                    if (yx_open) {
                        pause();
                    }
                    Intent intent = new Intent(GameActivity.this, PauseGameActivity.class);
                    intent.putExtra("close", "close");
                    startActivityForResult(intent, 700);
                }
                break;
        }
    }

    public void daojuTimes() {
        if (time_num.getVisibility() == View.VISIBLE && Integer.parseInt(time_num.getText().toString()) > 0) {
            //时间道具左右抖动
            animation_bg = AnimationUtils.loadAnimation(GameActivity.this, R.anim.time_anim);
            fragment_anim.setVisibility(View.VISIBLE);
            daoju_anim.setImageResource(R.drawable.time);
            // daoju_anim.setVisibility(View.VISIBLE);
            daoju_anim.startAnimation(animation_bg);
            animation_bg.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    isEable = false;
                    time.setEnabled(false);
                    zhadan.setEnabled(false);
                    tishi.setEnabled(false);
                    handler.removeCallbacks(progressRunnable);
                    if(yx_open) {
                        play( R.raw.time);
                    }
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    //daoju_anim.setVisibility(View.GONE);
                    fragment_anim.setVisibility(View.GONE);
                    isEable = true;
                    time.setEnabled(true);
                    zhadan.setEnabled(true);
                    tishi.setEnabled(true);
                    handler.post(jiatimeRunnable);
                    if(yx_open) {
                        stop();
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        } else {
            Intent intent = new Intent(GameActivity.this, PropsBuyActivity.class);
            intent.putExtra("daoju", "times");
            startActivityForResult(intent, 202);
            handler.removeCallbacks(progressRunnable);
            return;
        }
    }


    //提示道具
    public void daojuTishi() {
        if (tishi_num.getVisibility() == View.VISIBLE && Integer.parseInt(tishi_num.getText().toString()) > 0) {
            //提示道具由小变大
            animation_bg = AnimationUtils.loadAnimation(GameActivity.this, R.anim.game_daoju_anim);
            fragment_anim.setVisibility(View.VISIBLE);
            daoju_anim.setImageResource(R.drawable.use_tishi_xiaoguo);
            // daoju_anim.setVisibility(View.VISIBLE);
            daoju_anim.startAnimation(animation_bg);
            animation_bg.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    isEable = false;
                    time.setEnabled(false);
                    zhadan.setEnabled(false);
                    tishi.setEnabled(false);
                    handler.removeCallbacks(progressRunnable);
                    if(yx_open) {
                       play(R.raw.tishi);
                    }
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    //daoju_anim.setVisibility(View.GONE);
                    fragment_anim.setVisibility(View.GONE);
                    isEable = true;
                    time.setEnabled(true);
                    zhadan.setEnabled(true);
                    tishi.setEnabled(true);
                    handler.post(tishiRunable);
                    if(yx_open) {
                        stop();
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        } else {
            //Toast.makeText(GameActivity.this,"请购买之后再使用",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(GameActivity.this, PropsBuyActivity.class);
            intent.putExtra("daoju", "tishi");
            startActivityForResult(intent, 201);
            handler.removeCallbacks(progressRunnable);
            return;
        }
    }

    //炸弹道具
    public void daojuZhadan() {
        if (zhadan_num.getVisibility() == View.VISIBLE && Integer.parseInt(zhadan_num.getText().toString()) > 0) {
            //提示道具由小变大
            animation_bg = AnimationUtils.loadAnimation(GameActivity.this, R.anim.game_daoju_anim);
            fragment_anim.setVisibility(View.VISIBLE);
            daoju_anim.setImageResource(R.drawable.use_zhadan_xiaoguo);
            // daoju_anim.setVisibility(View.VISIBLE);
            daoju_anim.startAnimation(animation_bg);
            animation_bg.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    isEable = false;
                    time.setEnabled(false);
                    zhadan.setEnabled(false);
                    tishi.setEnabled(false);
                    handler.removeCallbacks(progressRunnable);
                    if(yx_open) {
                        play( R.raw.zhadan);
                    }
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    //daoju_anim.setVisibility(View.GONE);
                    fragment_anim.setVisibility(View.GONE);
                    isEable = true;
                    time.setEnabled(true);
                    zhadan.setEnabled(true);
                    tishi.setEnabled(true);
                    handler.post(zhadanRunnable);
                    if(yx_open) {
                        stop();
                    }
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        } else {
            //Toast.makeText(GameActivity.this,"请购买之后再使用",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(GameActivity.this, PropsBuyActivity.class);
            intent.putExtra("daoju", "zhandan");
            startActivityForResult(intent, 200);
            handler.removeCallbacks(progressRunnable);
            zhadan.setEnabled(false);
            return;
        }
    }

    private void setTiliNum() {
        if (status.equals("tili")) {
            String tili_num = GetPropLevelUtils.getInstance().getPropNum("体力");//(String) SharedPreferencesUtils.getParam(GameActivity.this, "tili_num", "");
            if (Integer.parseInt(tili_num) >= 1) {
                int newtili = Integer.parseInt(tili_num) - 1;
                //SharedPreferencesUtils.setParam(GameActivity.this, "tili_num", String.valueOf(newtili));
                Log.i("KYCC","game newtili === " + newtili);
                GetPropLevelUtils.getInstance().updatePropNum("体力",newtili);
            } else {
                //SharedPreferencesUtils.setParam(GameActivity.this, "tili_num", "0");
                GetPropLevelUtils.getInstance().updatePropNum("体力",0);
            }
        } else if (status.equals("xiyou")) {
            int xiyou = Integer.parseInt(GetPropLevelUtils.getInstance().getPropNum("西柚"));//(int) SharedPreferencesUtils.getParam(GameActivity.this, "xiyou", 0);
            if (xiyou >= 1) {
                int newXiyou = xiyou - 1;
                //SharedPreferencesUtils.setParam(GameActivity.this, "xiyou", newXiyou);
                GetPropLevelUtils.getInstance().updatePropNum("西柚",newXiyou);
            } else {
                //SharedPreferencesUtils.setParam(GameActivity.this, "xiyou", 0);
                GetPropLevelUtils.getInstance().updatePropNum("西柚",0);
            }
        }
    }

    Runnable tishiRunable = new Runnable() {
        @Override
        public void run() {
            if (imageList != null && imageList.size() > 0) {
                int id = imageList.get(0).getId();
                final ImageView RanImage = (ImageView) findViewById(id);
                for (int i = 0; i < intsLevel.length; i++) {
                    final ImageView Image = (ImageView) findViewById(i);
                    if (intsLevel[id] == intsLevel[i] && id != i && Image.getVisibility() == View.VISIBLE) {
                        Image.setImageResource(intsLevel[i]);
                        RanImage.setImageResource(intsLevel[id]);
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (Integer.parseInt(level_num) >= 14 && Integer.parseInt(level_num) < 25) {
                                    Image.setBackgroundResource(R.drawable.img_press_border);
                                    RanImage.setBackgroundResource(R.drawable.img_press_border);
                                } else {
                                    Image.setImageResource(R.drawable.image);
                                    RanImage.setImageResource(R.drawable.image);
                                }
                                //使用成功之后要减一,并保存
                                int ts_num = Integer.parseInt(tishi_num.getText().toString());
                                ts_num = ts_num - 1;
                                if (ts_num == 0) {
                                    tishi_num.setVisibility(View.INVISIBLE);
                                } else {
                                    tishi_num.setVisibility(View.VISIBLE);
                                }
                                tishi_num.setText(String.valueOf(ts_num));
                                //setParam(GameActivity.this, "tishi", ts_num);
                                GetPropLevelUtils.getInstance().updatePropNum("提示",ts_num);
                                alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"提示",ts_num);
                                handler.postDelayed(progressRunnable, 1000);
                                return;
                            }
                        }, 500);
                    }
                }
            } else {
                Clearance();
            }
        }
    };

    Runnable zhadanRunnable = new Runnable() {
        @Override
        public void run() {
            if (imageList != null && imageList.size() > 0) {
                    /*Random random = new Random();
                    int nextInt = random.nextInt(imageList.size()) + 1;*/
                int id = imageList.get(0).getId();
                final ImageView RanImage = (ImageView) findViewById(id);
                for (int i = 0; i < intsLevel.length; i++) {
                    final ImageView Image = (ImageView) findViewById(i);
                    if (intsLevel[id] == intsLevel[i] && id != i && Image.getVisibility() == View.VISIBLE) {
                        Image.setImageResource(intsLevel[i]);
                        RanImage.setImageResource(intsLevel[id]);
                        if (newbutton != null) {
                            newbutton.setBackgroundResource(R.drawable.img_normal_border);
                        } else if (isID != -1) {
                            ImageView view = (ImageView) findViewById(isID);
                            view.setBackgroundResource(R.drawable.img_normal_border);
                        }
                        Image.setBackgroundResource(R.drawable.img_press_border);
                        RanImage.setBackgroundResource(R.drawable.img_press_border);
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                Image.setVisibility(View.INVISIBLE);
                                RanImage.setVisibility(View.INVISIBLE);
                                Image.setBackgroundResource(R.drawable.img_normal_border);
                                RanImage.setBackgroundResource(R.drawable.img_normal_border);
                                fraction = fraction + 100;
                                //top_progressBar.setProgress(fraction);
                                getFraction(fraction);
                                imageList.remove(Image);
                                imageList.remove(RanImage);
                                inVisiableImage.add(Image);
                                inVisiableImage.add(RanImage);
                                //isVisiable++;
                                //使用成功之后要减一,并保存
                                int zd_num = Integer.parseInt(zhadan_num.getText().toString());
                                zd_num = zd_num - 1;
                                if (zd_num == 0) {
                                    zhadan_num.setVisibility(View.INVISIBLE);
                                } else {
                                    zhadan_num.setVisibility(View.VISIBLE);
                                }
                                zhadan_num.setText(String.valueOf(zd_num));
                                //setParam(GameActivity.this, "zhandan", zd_num);
                                GetPropLevelUtils.getInstance().updatePropNum("炸弹",zd_num);
                                alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"炸弹",zd_num);
                                if (Integer.parseInt(level_num) >= 10) {
                                    addImage(level_num, R.drawable.icecream_28, R.drawable.icecream_31, 4);
                                } else {
                                    addImage(level_num, R.drawable.icecream_28, R.drawable.icecream_30, 3);
                                }
                                handler.postDelayed(progressRunnable, 1000);
                                Clearance();
                                return;
                            }
                        }, 100);
                    }
                }
            } else {
                Clearance();
            }
        }
    };

    Runnable jiatimeRunnable = new Runnable() {
        @Override
        public void run() {
            if (progress >= 5) {
                progress = progress - 5;
                progressBar.setProgress(progress);
            } else {
                //progressBar.setMax(progressBar.getMax() + 5);
                progressBar.setProgress(0);
            }
            int tm_num = Integer.parseInt(time_num.getText().toString());
            tm_num = tm_num - 1;
            if (tm_num == 0) {
                time_num.setVisibility(View.INVISIBLE);
            } else {
                time_num.setVisibility(View.VISIBLE);
            }
            time_num.setText(String.valueOf(tm_num));
            //setParam(GameActivity.this, "time", tm_num);
            GetPropLevelUtils.getInstance().updatePropNum("时间",tm_num);
            alasbusyIApi.insertProps(GetPropLevelUtils.getInstance().getUserName(),"时间",tm_num);
            handler.postDelayed(progressRunnable, 1000);
            //handler.postDelayed(progressRunnable, 1000);
        }
    };

    public void play(final int raw){
        mPlayer = new MediaPlayer();
        Random random = new Random();
        int nextInt = random.nextInt(1);
        Log.i("KYCC","nextInt   ==== "+nextInt);
        AssetFileDescriptor file = getResources().openRawResourceFd(raw);
        try {
            mPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(),
                    file.getLength());
            mPlayer.prepare();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mPlayer.start();
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                play(raw);
            }
        });
    }

    public void stop(){
        if(mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
        }
    }

    public void pause(){
        mPlayer.pause();
    }

    @Override
    public void onError(Object e) {

    }

    @Override
    public void onSuccess(Object object) {
        if(object instanceof PropsCode) {
            PropsCode propsCode = (PropsCode) object;
            Log.i("KYCC",propsCode.getMessage());
        }
    }

    @Override
    public void onQuestErroCode(Object object) {
        if(object instanceof PropsCode) {
            PropsCode propsCode = (PropsCode) object;
            Toasty.error(GameActivity.this,propsCode.getMessage()).show();
        }
    }
}
